package cn.lingyangwl.agile.auth.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.lingyangwl.agile.auth.model.rqrs.ldp.*;
import cn.lingyangwl.agile.auth.service.ExtLdpService;
import cn.lingyangwl.agile.file.excel.CusExcelExportUtil;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.EmptyFileException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 外部身份源
 * 
 * @author shenguangyang
 */
@Slf4j
@Api(tags = "外部身份源")
@RestController
@RequestMapping("/extLdp")
public class ExtLdpController {
    @Resource
    private ExtLdpService extLdpService;

    @ApiOperation(value = "分页外部身份源")
    @GetMapping("/page")
    @RamResource(action = "auth:extLdp:query", name = "查询外部身份源", type = ActionType.READ)
    public Resp<IPage<ExtLdpResp>> pageExtLdp(@Validated ExtLdpPageReq req) {
        return Resp.ok(extLdpService.pageExtLdp(req));
    }

    @ApiOperation(value = "查询外部身份源")
    @GetMapping("/list")
    @RamResource(action = "auth:extLdp:query", name = "查询外部身份源", type = ActionType.READ)
    public Resp<List<ExtLdpResp>> listExtLdp(@Validated ExtLdpListReq req) {
        return Resp.ok(extLdpService.listExtLdp(req));
    }

    @ApiOperation(value = "通过id查找外部身份源")
    @GetMapping("/get")
    @RamResource(action = "auth:extLdp:query", name = "查询外部身份源", type = ActionType.READ)
    public Resp<ExtLdpResp> getExtLdp(@RequestParam(value = "code") String code) {
        return Resp.ok(extLdpService.getExtLdp(code));
    }
  
    @ApiOperation(value = "保存外部身份源")
    @PostMapping("/save")
    @RamResource(action = "auth:extLdp:save", name = "保存外部身份源", type = ActionType.WRITE)
    public Resp<?> saveExtLdp(@Validated @RequestBody ExtLdpSaveReq req) {
        return Resp.ok(extLdpService.saveExtLdp(req));
    }
  
    @ApiOperation(value = "更新外部身份源")
    @PostMapping("/update")
    @RamResource(action = "auth:extLdp:update", name = "更新外部身份源", type = ActionType.WRITE)
    public Resp<?> updateExtLdp(@Validated @RequestBody ExtLdpUpdateReq req) {
        return Resp.ok(extLdpService.updateExtLdp(req));
    }
  
    @ApiOperation(value = "批量删除外部身份源")
    @DeleteMapping("/batchDelete")
    @RamResource(action = "auth:extLdp:delete", name = "删除外部身份源", type = ActionType.WRITE)
    public Resp<?> batchDeleteExtLdp(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(extLdpService.batchDeleteExtLdp(ids));
    }
  
    @ApiOperation(value = "删除外部身份源")
    @DeleteMapping("/delete")
    @RamResource(action = "auth:extLdp:delete", name = "删除外部身份源", type = ActionType.WRITE)
    public Resp<?> deleteExtLdp(@RequestParam(value = "id") Long id) {
        return Resp.ok(extLdpService.deleteExtLdp(id));
    }

    @ApiOperation(value = "导出")
    @GetMapping("/exportExcel")
    @RamResource(action = "auth:extLdp:excel", name = "导出与导入身份源", type = ActionType.READ)
    public void exportExtLdp(HttpServletResponse response) {
        try {
            List<ExtLdpExcel> list = extLdpService.exportExtLdp();
            ExportParams params = new ExportParams();
            params.setType(ExcelType.HSSF);
            CusExcelExportUtil.exportExcel(response, params, "身份源.xls", ExtLdpExcel.class, list);
        } catch (Exception e) {
            log.error("error: ", e);
            throw new BizException("导出异常");
        }
    }

    @ApiOperation(value = "导入")
    @PostMapping("/importExcel")
    @RamResource(action = "auth:extLdp:excel", name = "导出与导入身份源", type = ActionType.WRITE)
    public Resp<?> importExtLdp(MultipartFile file) throws Exception {
        try {
            extLdpService.importExtLdp(file);
            return Resp.ok().msg("导入成功");
        } catch (EmptyFileException e) {
            return Resp.fail("文件为空, 不做导入操作");
        }
    }
}
